﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Runtime.Intrinsics.Arm;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 煤样化验任务
    /// </summary>
    public class SampleTestTaskService : TaskSchedulingAppService, ISampleTestTaskService
    {
        private IRepository<SampleTestTask, long> _sampleTestTaskRepository;

        public SampleTestTaskService(IRepository<SampleTestTask, long> sampleTestTaskRepository)
        {
            _sampleTestTaskRepository = sampleTestTaskRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<SampleTestTaskDto>> GetListAsync(SampleTestTaskInput input)
        {
            var iQuery = await _sampleTestTaskRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.SampleCode) && !string.IsNullOrEmpty(input.SampleType),
                i => i.SampleCode == input.SampleCode && i.SampleType == input.SampleType);
            iQuery = iQuery.Where(i => i.ManualFlag == 0);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<SampleTestTask>, List<SampleTestTaskDto>>(iQuery.ToList());

                return new PagedResultDto<SampleTestTaskDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<SampleTestTask>, List<SampleTestTaskDto>>(items.ToList());

                return new PagedResultDto<SampleTestTaskDto>(totalCount, itemsDto);
            }
        }
    }
}
